package com.cjl.blueBridgeCup.school;

import org.junit.jupiter.api.Test;

import java.util.*;

public class Match_School {

    @Test
    public void repeatThreeTime_7_1() {
        Scanner scan = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("I'm gonna WIN!");
        }
        scan.close();
    }

    @Test
    public void practice_7_2() {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int totalSum = 0;
        for (int i = 1; i <= n; i++) {
            int partSum = 1;
            for (int j = 1; j <= i; j++) {
                partSum *= j;
            }
            totalSum += partSum;
        }
        System.out.println(totalSum);
        scan.close();
    }

    @Test
    public void sortStr_7_1() {
        Scanner scan = new Scanner(System.in);
        String str = scan.nextLine();
        String[] strArr = str.split(" ");
        List<String> strList = Arrays.asList(strArr);
        Collections.sort(strList, new Comparator<String>() {
            @Override
            public int compare(String a, String b) {
                char[] arr_a = a.toCharArray();
                char[] arr_b = b.toCharArray();
                int len = Math.min(arr_a.length, arr_b.length);
                for (int i = 0; i < len; i++) {
                    if (arr_a[i] != arr_b[i]) {
                        return arr_a[i] - arr_b[i];
                    }
                }
                return 0;
            }
        });
        System.out.println("After sorted:");
        strList.forEach(System.out::println);
        scan.close();
    }

    @Test
    public void highest_7_5() {
        Scanner scan = new Scanner(System.in);
        int num = scan.nextInt();
        List<Student> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            String stuInfo = scan.nextLine();
            String[] split = stuInfo.split(" ");
            int dormitoryId = Integer.parseInt(split[0]);
            String name = split[1];
            int height = Integer.parseInt(split[2]);
            int weight = Integer.parseInt(split[3]);
            Student s = new Student(dormitoryId, name, height, weight);
            list.add(s);
        }
        // key为宿舍号，value为该宿舍最高的身高
        Map<Integer, Integer> map = new HashMap<>();
        for (Student s : list) {
            int id = s.getDormitoryId();
            // 如果map中存在则判断是否为最大值
            if (map.containsKey(id)) {
                Integer height = map.get(id);
                // 如果比map中的大则替换
                if (s.getHeight() > height) {
                    map.put(id, s.getHeight());
                }
            }
            // 不包含则加入
            map.put(id, s.getHeight());
        }
        Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
        for (Map.Entry<Integer, Integer> entry : entries) {
            Integer id = entry.getKey();
            Integer h = entry.getValue();
            for (Student s : list) {
                if (s.getDormitoryId() == id && s.getHeight() == h) {
                    int dormitoryId = s.getDormitoryId();
                    String name = s.getName();
                    int height = s.getHeight();
                    int weight = s.getWeight();
                    System.out.println(dormitoryId + " " + name + " " + height + " " + weight);
                }
            }
        }
        scan.close();
    }

    @Test
    public void highest_7_5_2() {
        Scanner scan = new Scanner(System.in);
        HashMap<Integer, String> map = new HashMap<>();
        int n = scan.nextInt();
        for (int i = 0; i < n; i++) {
            String info = scan.next();
            String[] split = info.split(" ");
            int dormitoryId = Integer.parseInt(split[0]);
            int height = Integer.parseInt(split[2]);
            // 不包含
            if (!map.containsKey(dormitoryId)) {
                map.put(dormitoryId, info);
            } else {
                // 包含
                int h = Integer.parseInt(map.get(dormitoryId).split(" ")[2]);
                if (height > h) {
                    map.put(dormitoryId, info);
                }
            }
            Set<Map.Entry<Integer, String>> entries = map.entrySet();
            for (Map.Entry<Integer, String> entry : entries) {
                System.out.println(entry.getValue());
            }
        }
        scan.close();
    }

    @Test
    public void single_7_6() {
        Scanner scan = new Scanner(System.in);
        int n = Integer.parseInt(scan.nextLine());
        ArrayList<Integer> list = new ArrayList<>();
        int[][] arr = new int[n][2];
        for (int i = 0; i < n; i++) {
            String couple = scan.nextLine();
            String[] split = couple.split(" ");
            arr[i][0] = Integer.parseInt(split[0]);
            arr[i][1] = Integer.parseInt(split[1]);
        }
        int k = Integer.parseInt(scan.nextLine());
        String single = scan.nextLine();
        for (String s : single.split(" ")) {
            list.add(Integer.parseInt(s));
        }
        ArrayList<Integer> singleList = new ArrayList<>();
        for (int s : list) {
            for (int i = 0; i < n; i++) {
                if (arr[i][0] == s && !list.contains(arr[i][1])) {
                    singleList.add(s);
                } else if (arr[i][1] == s && !list.contains(arr[i][0])) {
                    singleList.add(s);
                } else {
                    singleList.add(s);
                }
            }
        }
        System.out.println(singleList.size());
        for (int s : singleList) {
            System.out.println(s + " ");
        }
        scan.close();
    }

}

class Student {
    private int dormitoryId;
    private String name;
    private int height;
    private int weight;


    public Student() {
    }

    public Student(int dormitoryId, String name, int height, int weight) {
        this.dormitoryId = dormitoryId;
        this.name = name;
        this.height = height;
        this.weight = weight;
    }

    /**
     * 获取
     *
     * @return dormitoryId
     */
    public int getDormitoryId() {
        return dormitoryId;
    }

    /**
     * 设置
     *
     * @param dormitoryId
     */
    public void setDormitoryId(int dormitoryId) {
        this.dormitoryId = dormitoryId;
    }

    /**
     * 获取
     *
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     *
     * @return height
     */
    public int getHeight() {
        return height;
    }

    /**
     * 设置
     *
     * @param height
     */
    public void setHeight(int height) {
        this.height = height;
    }

    /**
     * 获取
     *
     * @return weight
     */
    public int getWeight() {
        return weight;
    }

    /**
     * 设置
     *
     * @param weight
     */
    public void setWeight(int weight) {
        this.weight = weight;
    }

    public String toString() {
        return "Student{dormitoryId = " + dormitoryId + ", name = " + name + ", height = " + height + ", weight = " + weight + "}";
    }
}
